package demo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-12-24
 * Time: 21:07
 */

//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}










//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}


//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}
//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}
//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}

//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}


public class Test {


    public static void func1(int[] array) {
        array = new int[10];
    }
    public static void func2(int[] array) {
        array[0] = 9;
    }

    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("abc",3);
        map.put("def",1);
        map.put("hello",4);
        map.put("ok",7);

        /*map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key "+ s +" value: "+integer);
            }
        });*/

        map.forEach((k,v) -> System.out.println("key: "+ k +" value: "+v));

    }



    public static void main6(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("abcd");
        list.add("efgh");
        /*list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });*/
        list.forEach(s -> System.out.println(s));
        System.out.println("++++++++++++++");
        /*list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });*/
        list.sort(((o1, o2) -> o1.compareTo(o2)));

        list.forEach(s -> System.out.println(s));
    }

    public static void main5(String[] args) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });



        PriorityQueue<Integer> maxHeap2 = new PriorityQueue<>((x,y)->{return x.compareTo(y);});

        PriorityQueue<Integer> maxHeap3 = new PriorityQueue<>((x,y)->x.compareTo(y));

    }

    public static void main4(String[] args) {
        int a = 10;
        //a = 1999;
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn(){
            @Override
            public void test() {
                //此时捕获的变量 一定是没有修改过得  或者是一个常量
                System.out.println("a = "+ a );
            }
        };

        noParameterNoReturn.test();


        NoParameterNoReturn noParameterNoReturn2 = ()->System.out.println("a ===> " + a);
        noParameterNoReturn2.test();
    }

    public static void main3(String[] args) {

        //MoreParameterReturn moreParameterReturn = (a,b) -> {return a+b;};
        MoreParameterReturn moreParameterReturn = (a,b) -> a+b;
        System.out.println(moreParameterReturn.test(10, 20));

        //NoParameterReturn noParameterReturn = ()->{return 10;};
        NoParameterReturn noParameterReturn = ()-> 10;
        int ret = noParameterReturn.test();
        System.out.println(ret);

        OneParameterReturn oneParameterReturn = x -> x*2;
        int ret2 = oneParameterReturn.test(12);
        System.out.println(ret2);

    }

    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = x-> System.out.println(x);
        oneParameterNoReturn.test(10);
        System.out.println("=======");
        MoreParameterNoReturn moreParameterNoReturn = (x,y) -> System.out.println(x+y);
        moreParameterNoReturn.test(10,20);
        System.out.println("=======");





    }
    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn(){
            @Override
            public void test() {
                System.out.println("1234");
            }
        };

        noParameterNoReturn.test();

        System.out.println("=================");

//        ()->{
//            System.out.println("1234");
//        };

        NoParameterNoReturn noParameterNoReturn2 = ()->System.out.println("1234");
        noParameterNoReturn2.test();


    }
}
